
#include <stdio.h>
#include "LaborCore.h"
#include "Renderer\LaborRenderSystem.h"
using namespace Labor;

//--------------------------------------------------------------------------------------
// Global Variables
//--------------------------------------------------------------------------------------
HINSTANCE               g_hInst = NULL;
HWND                    g_hWnd = NULL;

//--------------------------------------------------------------------------------------
// Forward declarations
//--------------------------------------------------------------------------------------
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK    WndProc( HWND, UINT, WPARAM, LPARAM );
void Render();

//int main()
//{
//
//	if ( pRenderSys )
//		pRenderSys->destroy();
//	getchar();
//	return 0;
//}
//--------------------------------------------------------------------------------------
// Entry point to the program. Initializes everything and goes into a message processing 
// loop. Idle time is used to render the scene.
//--------------------------------------------------------------------------------------
int  main( int argc, char* argv[] )
{
	//HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow
	//HINSTANCE hInstance = GetInstanceModule();

#ifdef _DEBUG
	GetLaborCore()->loadPlugin( L"LaborD3D10_Debug_Win32.dll");
#else
	GetLaborCore()->loadPlugin( L"LaborD3D10_Release_Win32.dll");
#endif
	ILRenderSystem* pRenderSys = GetLaborCore()->getRenderSystemByName(L"D3D10 Renderer");
	GetLaborCore()->setRenderSystem( pRenderSys );

	if ( NULL == pRenderSys )
		return 0;


	if( FAILED( InitWindow( (HINSTANCE)pRenderSys->getInst(), 0 ) ) )
		return 0;



	if( !pRenderSys->initDevice( (int)g_hWnd ) )
	{
		pRenderSys->cleanUpDevice();
		return 0;
	}

	// Main message loop
	MSG msg = {0};
	while( WM_QUIT != msg.message )
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			Render();
		}
	}

	pRenderSys->cleanUpDevice();

	return ( int )msg.wParam;
}


//--------------------------------------------------------------------------------------
// Register class and create window
//--------------------------------------------------------------------------------------
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{
	// Register class
	WNDCLASS wcex;
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )IDI_APPLICATION );
	wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
	wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = L"TestRender01";
	if( !RegisterClass( &wcex ) )
		return E_FAIL;

	// Create window
	g_hInst = hInstance;
	RECT rc = { 0, 0, 640, 480 };
	AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
	g_hWnd = CreateWindow( L"TestRender01", L"TestRender01: Test Render", WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
		NULL );
	if( !g_hWnd )
		return E_FAIL;

	ShowWindow( g_hWnd, TRUE );

	return S_OK;
}

//--------------------------------------------------------------------------------------
// Called every time the application receives a message
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch( message )
	{
	case WM_PAINT:
		hdc = BeginPaint( hWnd, &ps );
		EndPaint( hWnd, &ps );
		break;

	case WM_DESTROY:
		PostQuitMessage( 0 );
		break;

	default:
		return DefWindowProc( hWnd, message, wParam, lParam );
	}

	return 0;
}

//--------------------------------------------------------------------------------------
// Render the frame
//--------------------------------------------------------------------------------------
void Render()
{
	// Just clear the backbuffer
	ILRenderSystem* pRenderSys = GetLaborCore()->getRenderSystemByName(L"D3D10 Renderer");

	if ( NULL == pRenderSys )
		return;
	pRenderSys->clearBackBuffer(0.0f, 0.125f, 0.3f, 1.0f);
	pRenderSys->present(0, 0);
}


